package woa.tree.binary;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * 对称二叉树
 * 双指针！！！
 *
 * @author wangpeng
 * @date 2021/12/3
 */
public class IsSymmetric {

    public static boolean isSymmetric(TreeNode root) {
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.offer(root);
        int level = 1;
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> list = new ArrayList<>((int) Math.pow(2, level));
            for (int i = 0; i < size; i++) {
                TreeNode cur = queue.poll();
                if (cur.left != null) {
                    queue.offer(cur.left);
                    list.add(cur.left.val);
                } else {
                    list.add(null);
                }

                if (cur.right != null) {
                    queue.offer(cur.right);
                    list.add(cur.right.val);
                } else {
                    list.add(null);
                }
            }
            if (!arrIsSymmetric(list)) {
                return false;
            }
            level++;
        }

        return true;
    }

    public static boolean arrIsSymmetric(List<Integer> list) {
        int i = 0, j = list.size() - 1;
        while (i < j) {
            if (list.get(i) != list.get(j)) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1, new TreeNode(0), null);
        System.out.println(isSymmetric(root));
    }
}
